# -*- coding: utf-8 -*-
# @Time    : 2021/11/8 21:33
# @Author  : YIGE
# @Email   : yige@maqu.com
# @File    : tasks.py
# @Software: PyCharm
import logging

# Get an instance of a logger
from django.core.mail import send_mail

from system.settings import EMAIL_HOST_USER
from celery_tasks.celery_main import app

logger = logging.getLogger(__name__)


@app.task(name="send_mail_task")
def send_mail_task(email, username, password, classes_name):
    """
    发送通知邮件
    :param email:
    :param username:
    :param password:
    :param classes_name:
    :return:
    """
    try:
        email_param = {
            'subject': '学生管理系统',
            'message': '',
            'html_message':
                f"""
                    <h2>欢迎加入{classes_name}]进行学习</h2>
                    <h3>诚邀您使用</h3>
                    <p>您的登陆用户名为：<span style='color:#116bb7'>{username}</span></p>
                    <p>您的登陆初始密码为：<span style='color:#116bb7'>{password}</span>，登陆后建议立即修改</p>
                    <p><a href='#'>点击访问学生管理系统</a></p>
                """,
            'from_email': EMAIL_HOST_USER,
            'recipient_list': [email],
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error("验证码邮件发送[异常][ email: %s, message: %s ]" % (email, e))
    else:
        if res_email != 1:
            logger.warning("验证码邮件发送[失败][ email: %s ]" % email)
        else:
            logger.info("验证码邮件发送[正常][ email: %s, username: %s ]" % (email, username))
